import 'package:get/get.dart';
import 'package:oli/services/Apis.dart';
import 'package:oli/bean/ApiResponse.dart';
import 'package:oli/bean/FuelRecord.dart';
import 'package:oli/services/ApiService.dart';
import 'package:oli/utils/Util.dart';

import 'state.dart';

class AddRecordLogic extends GetxController {
  final AddRecordState state = AddRecordState();

  void saveFuelRecord() async {
    FuelRecordData dto = toData();
    ApiResponse rest = await ApiService.postRequest(
      Apis.init,
      dto.toJson() as Map<String, dynamic>,
    );
    if (rest.code == 2000) {
      toastCenter("新增记录成功！");
    }
  }

  // 将 Rx 变量的值提取出来生成一个普通的 FuelRecordData 对象
  FuelRecordData toData() {
    return FuelRecordData(
      refuelingTime: state.refuelingTime.value,
      totalMileage: state.totalMileage.value,
      remainingMileage: state.remainingMileage.value,
      refuelingCost: state.refuelingCost.value,
      discountAmount: state.discountAmount.value,
      paymentAmount: state.paymentAmount.value,
      oilPrice: state.oilPrice.value,
      refuelingVolume: state.refuelingVolume.value,
      isLastRecorded: state.isLastRecorded.value,
    );
  }
}
